package com.tedu.egspringbootmvc.controller;

import com.tedu.egspringbootmvc.common.result.JsonResult;
import com.tedu.egspringbootmvc.common.result.StatusCode;
import com.tedu.egspringbootmvc.mapper.UserMapper;
import com.tedu.egspringbootmvc.pojo.dto.UserLoginDTO;
import com.tedu.egspringbootmvc.pojo.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("/v1/users/login")
    public JsonResult login(@RequestBody UserLoginDTO userLoginDTO , @ApiIgnore HttpSession session) {
        UserVO userVO=userMapper.selectUser(userLoginDTO.getUsername());
        if(userVO!=null){
            if(userVO.getPassword().equals(userLoginDTO.getPassword())){
                session.setAttribute("user",userVO);
                return new JsonResult(StatusCode.OPERATION_SUCCESS);
            }else {
                return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
            }
        }else {
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
    }
}
